module Base
  class API < Grape::API

    format :json
    prefix 'api'
    rescue_from :all do |error|
      Rack::Response.new({code: 40000, message: error.as_json}.to_json, 200, {'Content-Type' => 'application/json;charset=UTF-8'})
    end
    rescue_from StandardError do |error|
      Rack::Response.new(error.as_json.to_json, 200, {'Content-Type' => 'application/json;charset=UTF-8'})
    end
    rescue_from APIError do |error|
      Rack::Response.new({code: 40000, message: error.as_json}.to_json, 200, {'Content-Type' => 'application/json;charset=UTF-8'})
    end
    rescue_from ActiveRecord::RecordInvalid do |error|
      Rack::Response.new({code: 40000, message: error.full_message}.to_json, 200, {'Content-Type' => 'application/json;charset=UTF-8'})
    end

    rescue_from Grape::Exceptions::ValidationErrors do |error|
      Rack::Response.new({code: 40000, message: error.message}.to_json, 200, {'Content-Type' => 'application/json;charset=UTF-8'})
    end
    
    helpers Base::Helper
    mount V1::API
  end
end
